//
//  ViewController.swift
//  DZYPageView
//
//  Created by dongzhenyu on 2017/7/6.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

import UIKit

private let kCollectionViewCellID = "kCollectionViewCellID"

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        automaticallyAdjustsScrollViewInsets = false
        
//        // 创建DYZPageView
//        // 1. 获取pageView的frame
//        let frame = CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height - 64)
//        
//        // 2. 获取pageView显示的标题
//        let titles = ["推荐", "游戏游戏游戏游戏", "娱乐游戏", "趣玩", "推荐", "游戏游戏游戏游戏", "娱乐游戏", "趣玩"]
//        
//        // 3.获取pageView里所有的子控制器
//        var childVcs = [UIViewController]()
//        
//        for _ in 0..<titles.count {
//            let vc = UIViewController()
//            vc.view.backgroundColor = UIColor.randomColor()
//            childVcs.append(vc)
//        }
//        
//        // 4. 样式
//        var style = DZYPageStyle()
//        style.isScrollEnabel = true
////        style.isNeedScale = true
//        style.isShowBottonLine = false
//        style.isShowCover = true
//        
//        let pageView = DZYPageView(frame: frame, titles: titles, style: style, childVcs: childVcs, parentVc: self)
//        
//        view.addSubview(pageView)
        
        
        let pageFrame = CGRect(x: 0, y: 100, width: view.bounds.width, height: 300)
        
        let titles = ["热门", "高级", "专属", "豪华"]
        
        var style = DZYPageStyle()
        style.isShowBottonLine = true
        
        let layout = DZYPageCollectionLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemMargin = 5
        layout.lineMargin = 5
        layout.cols = 7
        layout.rows = 3
        
        let pageCollectionView = DZYPageCollectionView(frame: pageFrame, titles: titles, style: style, layout: layout)
        
        pageCollectionView.dataSource = self
        pageCollectionView.registerCell(UICollectionViewCell.self, reusableIdentifier: kCollectionViewCellID)
        
        view.addSubview(pageCollectionView)
        
        
    }
}

extension ViewController: DZYPageCollectionViewDataSource {
    func numberOfSectionInPageCollectionView(_ pageCollectionView: DZYPageCollectionView) -> Int {
        return 4
    }
    
    func pageCollectionView(_ pageCollectionView: DZYPageCollectionView, numberOfItemsSection section: Int) -> Int {
        
        if section == 0 {
            return 60
        } else if section == 1 {
            return 18
        } else if section == 2 {
            return 40
        } else {
            // 最后一组 并且该组只有一页时候 有个位置偏移的小bug 
            return 10
        }
    }
    
    func pageCollectionView(_ pageCollectionView: DZYPageCollectionView, _ collctionView: UICollectionView, cellAtIndexPath indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collctionView.dequeueReusableCell(withReuseIdentifier: kCollectionViewCellID, for: indexPath)
        cell.backgroundColor = UIColor.randomColor()
        return cell
    }
}


